package com.xsy.android.compose.we.component.ui.compose

import android.util.Log
import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.compose.rememberNavController
import com.xsy.android.compose.we.component.R
import com.xsy.android.compose.we.component.ui.theme.WeComposeTheme

/**
 *@author: xiaosy
 *时间: 2024/6/26
 *@Description: 底部导航栏
 *@Version: 1.0
 **/
@Composable
fun BottomNavigation(current: Int, currentChanged: (Int) -> Unit) {

    val navList = listOf("聊天", "通讯录", "发现", "我")
    val navIconList = listOf(
        R.drawable.ic_chat_outlined,
        R.drawable.ic_contacts_outlined,
        R.drawable.ic_discover_outlined,
        R.drawable.ic_me_outlined
    )
    val navSelectedIconList = listOf(
        R.drawable.ic_chat_filled,
        R.drawable.ic_contacts_filled,
        R.drawable.ic_discover_filled,
        R.drawable.ic_me_filled
    )
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(WeComposeTheme.colors.bottomBar)
            .padding(4.dp, 0.dp)
            .navigationBarsPadding()
    ) {
        navList.forEachIndexed { index, item ->
            run {
                BottomNavigationItem(
                    Modifier
                        .weight(1f)
                        .clickable {
                            currentChanged(index)
                        },
                    iconId = if (current == index) navSelectedIconList[index]
                    else navIconList[index],
                    title = item,
                    tint = if (current == index) WeComposeTheme.colors.iconCurrent
                    else WeComposeTheme.colors.icon
                )
            }
        }
    }
}

@Composable
fun BottomNavigationItem(
    modifier: Modifier = Modifier, @DrawableRes iconId: Int, title: String, tint: Color
) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = modifier.padding(0.dp, 8.dp, 0.dp, 8.dp)
    ) {
        Image(
            imageVector = ImageVector.vectorResource(iconId),
            contentDescription = title,
            modifier = Modifier.size(24.dp),
            colorFilter = ColorFilter.tint(tint)
        )
        Text(text = title, fontSize = 12.sp, color = tint)
    }
}

@Preview(showBackground = true)
@Composable
fun PreviewBottomNavigation() {
    var current by remember {
        mutableIntStateOf(0)
    }
    BottomNavigation(current) { selectedIndex ->
        current = selectedIndex
    }
}